recvfrom function (winsock.h)

您所在的位置:网站首页 winsock2 udp recvfrom function (winsock.h)

recvfrom function (winsock.h)

#recvfrom function (winsock.h)| 来源: 网络整理| 查看: 265

recvfrom function (winsock.h) Article 10/13/2021 7 minutes to read

The recvfrom function receives a datagram and stores the source address.

Syntax int recvfrom( [in] SOCKET s, [out] char *buf, [in] int len, [in] int flags, [out] sockaddr *from, [in, out, optional] int *fromlen ); Parameters

[in] s

A descriptor identifying a bound socket.

[out] buf

A buffer for the incoming data.

[in] len

The length, in bytes, of the buffer pointed to by the buf parameter.

[in] flags

A set of options that modify the behavior of the function call beyond the options specified for the associated socket. See the Remarks below for more details.

[out] from

An optional pointer to a buffer in a sockaddr structure that will hold the source address upon return.

[in, out, optional] fromlen

An optional pointer to the size, in bytes, of the buffer pointed to by the from parameter.

Return value

If no error occurs, recvfrom returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Error code Meaning WSANOTINITIALISED A successful WSAStartup call must occur before using this function. WSAENETDOWN The network subsystem has failed. WSAEFAULT The buffer pointed to by the buf or from parameters are not in the user address space, or the fromlen parameter is too small to accommodate the source address of the peer address. WSAEINTR The (blocking) call was canceled through WSACancelBlockingCall. WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. WSAEINVAL The socket has not been bound with bind, or an unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled, or (for byte stream-style sockets only) len was zero or negative. WSAEISCONN The socket is connected. This function is not permitted with a connected socket, whether the socket is connection oriented or connectionless. WSAENETRESET For a datagram socket, this error indicates that the time to live has expired. WSAENOTSOCK The descriptor in the s parameter is not a socket. WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not stream-style such as type SOCK_STREAM, OOB data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only send operations. WSAESHUTDOWN The socket has been shut down; it is not possible to recvfrom on a socket after shutdown has been invoked with how set to SD_RECEIVE or SD_BOTH. WSAEWOULDBLOCK The socket is marked as nonblocking and the recvfrom operation would block. WSAEMSGSIZE The message was too large to fit into the buffer pointed to by the buf parameter and was truncated. WSAETIMEDOUT The connection has been dropped, because of a network failure or because the system on the other end went down without notice. WSAECONNRESET The virtual circuit was reset by the remote side executing a hard or abortive close. The application should close the socket; it is no longer usable. On a UDP-datagram socket this error indicates a previous send operation resulted in an ICMP Port Unreachable message. Remarks

The recvfrom function reads incoming data on both connected and unconnected sockets and captures the address from which the data was sent. This function is typically used with connectionless sockets. The local address of the socket must be known. For server applications, this is usually done explicitly through bind. Explicit binding is discouraged for client applications. For client applications using this function, the socket can become bound implicitly to a local address through sendto, WSASendTo, or WSAJoinLeaf.

For stream-oriented sockets such as those of type SOCK_STREAM, a call to recvfrom returns as much information as is currently available鈥攗p to the size of the buffer specified. If the socket has been configured for inline reception of OOB data (socket option SO_OOBINLINE) and OOB data is yet unread, only OOB data will be returned. The application can use the ioctlsocket or WSAIoctlSIOCATMARK command to determine whether any more OOB data remains to be read. The from and fromlen parameters are ignored for connection-oriented sockets.

For message-oriented sockets, data is extracted from the first enqueued message, up to the size of the buffer specified. If the datagram or message is larger than the buffer specified, the buffer is filled with the first part of the datagram, and recvfrom generates the error WSAEMSGSIZE. For unreliable protocols (for example, UDP) the excess data is lost. For UDP if the packet received contains no data (empty), the return value from the recvfrom function function is zero.

If the from parameter is nonzero and the socket is not connection oriented, (type SOCK_DGRAM for example), the network address of the peer that sent the data is copied to the corresponding sockaddr structure. The value pointed to by fromlen is initialized to the size of this structure and is modified, on return, to indicate the actual size of the address stored in the sockaddr structure.

If no incoming data is available at the socket, the recvfrom function blocks and waits for data to arrive according to the blocking rules defined for WSARecv with the MSG_PARTIAL flag not set unless the socket is nonblocking. In this case, a value of SOCKET_ERROR is returned with the error code set to WSAEWOULDBLOCK. The select, WSAAsyncSelect, or WSAEventSelect can be used to determine when more data arrives.

If the socket is connection oriented and the remote side has shut down the connection gracefully, the call to recvfrom will complete immediately with zero bytes received. If the connection has been reset recvfrom will fail with the error WSAECONNRESET.

The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket. The semantics of this function are determined by the socket options and the flags parameter. The latter is constructed by using the bitwise OR operator with any of the following values.

Value Meaning MSG_PEEK Peeks at the incoming data. The data is copied into the buffer but is not removed from the input queue. MSG_OOB Processes Out Of Band (OOB) data. 聽 Note聽聽When issuing a blocking Winsock call such as recvfrom, Winsock may need to wait for a network event before the call can complete. Winsock performs an alertable wait in this situation, which can be interrupted by an asynchronous procedure call (APC) scheduled on the same thread. Issuing another blocking Winsock call inside an APC that interrupted an ongoing blocking Winsock call on the same thread will lead to undefined behavior, and must never be attempted by Winsock clients. 聽 Example Code The following example demonstrates the use of the recvfrom function. #ifndef UNICODE #define UNICODE #endif #define WIN32_LEAN_AND_MEAN #include #include #include // Link with ws2_32.lib #pragma comment(lib, "Ws2_32.lib") int main() { int iResult = 0; WSADATA wsaData; SOCKET RecvSocket; struct sockaddr_in RecvAddr; unsigned short Port = 27015; char RecvBuf[1024]; int BufLen = 1024; struct sockaddr_in SenderAddr; int SenderAddrSize = sizeof (SenderAddr); //----------------------------------------------- // Initialize Winsock iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != NO_ERROR) { wprintf(L"WSAStartup failed with error %d\n", iResult); return 1; } //----------------------------------------------- // Create a receiver socket to receive datagrams RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (RecvSocket == INVALID_SOCKET) { wprintf(L"socket failed with error %d\n", WSAGetLastError()); return 1; } //----------------------------------------------- // Bind the socket to any address and the specified port. RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons(Port); RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY); iResult = bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr)); if (iResult != 0) { wprintf(L"bind failed with error %d\n", WSAGetLastError()); return 1; } //----------------------------------------------- // Call the recvfrom function to receive datagrams // on the bound socket. wprintf(L"Receiving datagrams...\n"); iResult = recvfrom(RecvSocket, RecvBuf, BufLen, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize); if (iResult == SOCKET_ERROR) { wprintf(L"recvfrom failed with error %d\n", WSAGetLastError()); } //----------------------------------------------- // Close the socket when finished receiving datagrams wprintf(L"Finished receiving. Closing socket.\n"); iResult = closesocket(RecvSocket); if (iResult == SOCKET_ERROR) { wprintf(L"closesocket failed with error %d\n", WSAGetLastError()); return 1; } //----------------------------------------------- // Clean up and exit. wprintf(L"Exiting.\n"); WSACleanup(); return 0; }

Windows聽Phone聽8: This function is supported for Windows Phone Store apps on Windows聽Phone聽8 and later.

Windows聽8.1 and Windows Server聽2012聽R2: This function is supported for Windows Store apps on Windows聽8.1, Windows Server聽2012聽R2, and later.

Requirements 聽 聽 Minimum supported client Windows聽8.1, Windows聽Vista [desktop apps | UWP apps] Minimum supported server Windows Server聽2003 [desktop apps | UWP apps] Target Platform Windows Header winsock.h (include Winsock2.h) Library Ws2_32.lib DLL Ws2_32.dll See also

WSAAsyncSelect

WSAEventSelect

Winsock Functions

Winsock Reference

recv

send

sockaddr

socket



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3